<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage Product
 *
 * @package     manage
 * @subpackage  
 * @category    Product -  产品管理
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Product extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation'); // 表单验证类
		$this->load->model('category_model');    // 加载分类模型
		$this->load->model('product_model');     // 加载产品模型 
		check_login(); // 检查是否登录
	}


	public function index(){
		$total = $this->product_model->get_num_rows();
		if($total != 0){
			// 加载分页类
			$arr = array('page_total_num' => $total,'every_page_num'=>10,'page_url_name'=>'page_id');
			$this->load->library('page',$arr);
			$data['pager']	  = $this->page->pages_all();
			$data['arr'] 	  = $this->product_model->get_all_data_and_page($this->page->limit);
		}else{
			$data['error'] = '暂无产品!';
		}
		$this->load->view('admin/product/index',$data);
	}

	/**
	 * [insert 增加产品]
	 * @return [type] [description]
	 */
	public function insert(){
		$data['category'] = $this->category_model->to_options(0,0); // 栏目数据
		if ($this->form_validation->run('product') == FALSE){
		  	$this->load->view('admin/product/insert',$data);
		}else{
			if(!empty($_POST['name'])){
				$arr = $this->input->post();
				$_insert['pro_pid'] 	= $arr['pid'];
				$_insert['pro_name'] 	= $arr['name'];
				$_insert['pro_price']	= $arr['price'];
				$_insert['pro_profile']	= $arr['profile'];
				$_insert['pro_remark']	= $arr['remark'];
				$_insert['pro_desc']	= $arr['desc'];
				$_insert['pro_state']	= $arr['state'];
				$_insert['pro_sort']	= $arr['sort'];
				if($this->product_model->insert($_insert)){
					$this->sys_message->skip('增加产品成功',site_url('manage/product'));
				}else{
					$this->sys_message->show_error('增加产品失败,请重新添加！');
				}
			}
		}
	}

	public function update(){
		$data['proarr']   = $this->product_model->get_pro_by_aid($_GET['id']);
		$data['category'] = $this->category_model->to_options(0,0,$data['proarr']['pro_pid']); // 栏目数据
		if ($this->form_validation->run('product') == FALSE){
		  	$this->load->view('admin/product/update',$data);
		}else{
			if(!empty($_POST['name'])){
				$arr = $this->input->post();
				$_update['pro_pid'] 	= $arr['pid'];
				$_update['pro_name'] 	= $arr['name'];
				$_update['pro_price']	= $arr['price'];
				$_update['pro_profile']	= $arr['profile'];
				$_update['pro_remark']	= $arr['remark'];
				$_update['pro_desc']	= $arr['desc'];
				$_update['pro_state']	= $arr['state'];
				$_update['pro_sort']	= $arr['sort'];
				if($this->product_model->update($_update,$_GET['id'])){
					$this->sys_message->skip('编辑产品成功',site_url('manage/product'));
				}else{
					$this->sys_message->show_error('编辑产品失败,请联系网站管理员！');
				}
			}
		}
	}
	/**
	 * [delete 删除产品]
	 * @return [type] [description]
	 */
	public function delete(){
		if(!empty($_GET['id']) && intval($_GET['id'])){
			if($this->product_model->delete($_GET['id'])){
				die('ok');
			}else{
				die('no');
			}
		}else{
			die('error_1');
		}
	}
}